package me.zhengjie.tool;

import com.google.common.collect.Maps;
import me.zhengjie.vo.Constant;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基数计算基础类
 *
 * @author zhangc
 * @since 2021/4/23 9:57
 */
public class SalaryParametersUtil {

    /**
     * 获取基数
     *
     * @param aab359
     * @param aae140
     * @param aaz136
     * @param aae003
     * @param aac040
     * @return
     */
    public static BigDecimal dealCalculateAae180(String aab359, String aae140, String aaz136, String baz137, Integer aae003, BigDecimal aac040, MyMap aa06MyMap, MyMap aa29, List<MyMap> aa02MyMaps) {
        Map<String, BigDecimal> bigDecimalMap = getAae180(aab359, aae140, aaz136, baz137, aae003, aa06MyMap, aa29, aa02MyMaps);
        NPETool.isEmptyThrowException(bigDecimalMap, "保底封顶基数");
        BigDecimal aae180_min = bigDecimalMap.get("aae180_bd");
        BigDecimal aae180_max = bigDecimalMap.get("aae180_fd");
        BigDecimal aae180;
        if (aac040.compareTo(aae180_min) < 0) {
            aae180 = aae180_min;
        } else if (aac040.compareTo(aae180_max) > 0) {
            aae180 = aae180_max;
        } else {
            aae180 = aac040;
        }
        //added by fancl 2019/4/11 基数保底封顶后四舍五入到十位
//        aae180 = aae180.setScale(-1, BigDecimal.ROUND_HALF_UP);  // 四舍五入 >=5 进位
        if (aae180.compareTo(aae180_min) < 0) {
            return aae180.add(new BigDecimal(10));
        } else if (aae180.compareTo(aae180_max) > 0) {
            return aae180.subtract(new BigDecimal(10));
        } else {
            return aae180;
        }
    }

    //获取对应aa02
    public static MyMap getAa02(List<MyMap> aa02MyMaps, Integer year, Integer aae003, String baz137) {
        if (Constant.BAZ137_1_DNANQZXGZ.equals(baz137) || Constant.BAZ137_2_LNANQZXGZ.equals(baz137)
                || Constant.BAZ137_5_SSNGZ.equals(baz137)) {
            year = year - 1;//取所属年度上年社平工资
            //按年取社平工资
            Map<Integer, List<MyMap>> datamap = aa02MyMaps.stream().collect(
                    Collectors.groupingBy(aa02MyMap -> aa02MyMap.getAsInteger("aae001")));

            List<MyMap> aa02maps = datamap.get(year);
            while (ValidateUtil.isEmpty(aa02maps)) {
                year--;
                aa02maps = datamap.get(year);
                if (year <= 1950) {
                    break;
                }
            }
            if (ValidateUtil.isNotEmpty(aa02maps)) {
                return aa02maps.get(0);
            }

        } else if (Constant.BAZ137_3_DNASYFWQGZ.equals(baz137) || Constant.BAZ137_4_LNASYFWQGZ.equals(baz137)) {
            for (MyMap myMap : aa02MyMaps) {
                if (DateUtil.equalsDate(myMap, aae003)) {
                    return myMap;
                }
            }
        }
        return new MyMap();
    }


    //获取aa06
    public static MyMap getAa06(List<MyMap> aa06MyMaps, String aab359, String aae140, String aaz136, Integer aae003) {
        String[] aab359s = {"$$$$$$", aab359.substring(1, 2) + "$$$$", aab359.substring(1, 4) + "$$"};
        for (MyMap myMap : aa06MyMaps) {
            if ((Arrays.asList(aab359s).contains(myMap.getAsString("aab359")) || aab359.equals(myMap.getAsString("aab359")))
                    && (aae140.equals(myMap.getAsString("aae140")))
                    && aaz136.equals(myMap.getAsString("aaz136"))) {
                //日期匹配
                if (DateUtil.equalsDate(myMap, aae003)) {
                    return myMap;
                }
            }
        }
        throw new IllegalArgumentException("基数规则aab359:" + aab359 + "|aae140:" + aae140 + "|aaz136:" + aaz136);
    }


    /**
     * 查询保底封顶基数
     *
     * @return
     */
    public static Map<String, BigDecimal> getAae180(String aab359, String aae140, String aaz136, String baz137, Integer aae003, MyMap aa06MyMap, MyMap aa29, List<MyMap> aa02MyMaps) {
        Map<String, BigDecimal> aae180map = Maps.newHashMap();
        NPETool.isEmptyThrowException(aab359, "参保分中心");
        NPETool.isEmptyThrowException(aae140, "险种类型");
        NPETool.isEmptyThrowException(aaz136, "基数规则ID");
        NPETool.isEmptyThrowException(aae003, "费款所属期");
        //处理保底封顶基数
        BigDecimal aaa125 = aa06MyMap.getAsBigDecimal("aaa125");
        if (!ValidateUtil.isEmpty(aaa125)) {
            aae180map.put("aae180_bd", aaa125);
            aae180map.put("aae180_fd", aaa125);
            return aae180map;
        }
        //获取业务年度
        Integer year = aa29.getAsInteger("aae001");
        String baa008 = aa06MyMap.getAsString("baa008");//下限
        //获取保底工资
        aae180map.put("aae180_bd", getAaa010(baa008, aae003, baz137, year, aa02MyMaps));
        String baa009 = aa06MyMap.getAsString("baa009");//上限
        aae180map.put("aae180_fd", getAaa010(baa009, aae003, baz137, year, aa02MyMaps));
        return aae180map;
    }

    //获取aa02对应工资
    public static BigDecimal getAaa010(@NotNull String baa010, @NotNull Integer aae003, String baz137, Integer year, List<MyMap> aa02MyMaps) {
        //基于对应baa010获取到aa02MyMaps
        List<MyMap> aa02Info = aa02MyMaps.stream().filter(myMap -> baa010.equals(myMap.getAsString("baa010"))).collect(Collectors.toList());
        NPETool.isEmptyThrowException(aa02Info, "AA02参数");
        MyMap aa02 = getAa02(aa02Info, year, aae003, baz137);
        return aa02.getAsBigDecimal("aaa010");
    }



}
